package com.kensure.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;

import com.alibaba.fastjson.JSONObject;
import com.kensure.mycom.config.model.MyConfig;
import com.kensure.mycom.config.service.MyConfigService;
import com.kensure.shike.weixin.model.WeixinSign;
import com.kensure.shike.weixin.service.WeixinSignService;

/**
 * 系统管理部分接口
 * 
 */
@Controller
@RequestMapping(value = "sysmanager")
public class SysManagerController {

	@Resource
	private MyConfigService myConfigService;

	@Resource
	private WeixinSignService weixinSignService;

	/**
	 * 重置配置缓存
	 */
	@ResponseBody
	@RequestMapping(value = "initsysconfig.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo initsysconfig(HttpServletRequest req, HttpServletResponse rep) {
		myConfigService.initCache();
		return new ResultInfo();
	}

	/**
	 * 获取配置
	 */
	@ResponseBody
	@RequestMapping(value = "getconfig.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo getconfig(HttpServletRequest req, HttpServletResponse rep) {
		String ids = req.getParameter("ids");
		List<MyConfig> list = MyConfigService.getMultiMyConfig(ids);
		return new ResultRowsInfo(list);
	}


	/**
	 * 初始化
	 */
	@ResponseBody
	@RequestMapping(value = "initcache.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo initcache(HttpServletRequest req, HttpServletResponse rep) {
		myConfigService.initCache();
		return new ResultInfo();
	}

	/**
	 * 微信配置数据，给前端调用微信接口用
	 */
	@ResponseBody
	@RequestMapping(value = "weixinsign.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
	public ResultInfo weixinsign(HttpServletRequest req, HttpServletResponse rep) {
		JSONObject json = RequestUtils.paramToJson(req);
		String url = json.getString("url");
		WeixinSign sign = weixinSignService.getSign(url);
		return new ResultRowInfo(sign);
	}

}
